package crudfx.container;

import crudfx.control.*;
import javafx.scene.*;
import javafx.scene.input.*;
import javafx.geometry.*;

/**
 * @profile desktop
 * @author Sergey Surikov
 */
public class Desk extends CustomNode{
    public var content:DeskPanel[] on replace {
		refill();
	    };
    var group:Group=Group {};
    var lock:Boolean=true;
    function refill():Void{
	if(lock){
	    return ;
	    }
	delete  group.content;
	for(node:DeskPanel in content){
	    if(node.content.parent!=null){
		try{
		    var g:Group=node.content.parent as Group;
		    delete node.content from g.content;
		}
		catch(x){
		    x.printStackTrace();
		}
	    }
	    var me:Dragger;
	    def t:DeskPanel=node;
	    insert me=Dragger {
		x:bind t.x with inverse
		y:bind t.y with inverse
		content:Group {
		    content:node.content
		    onMousePressed:function (e:MouseEvent):Void{
			me.toFront();
			lock=true;
			delete node from content;
			insert node into content;
			lock=false;
		    }
		}
	    } into group.content;
	}
    }
    public override function create():Node{
	lock=false;
	refill();
	return Group {content:bind group};
    }
}
